<template>
    <div class="festivalContainer">

        <div class="contentDiv">
            <div class="titleDiv">
                <span style="margin-right: 20px;margin-left:15px;font-weight: bold;">节日管理</span>
                <el-input v-model="dataForm.key" placeholder="请输入任务名称" clearable style="width: 150px;margin-right: 10px;"
                    @keyup.enter.native="handleQuery" size="small" />
                <el-button type="primary" icon="el-icon-search" size="mini" @click="getDataList()">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </div>

            <div class="toolDiv">
                <el-button type="text" size="small" @click="addOrUpdateHandle()">
                    <i class="iconfont icon-tianjia" style="margin-right: 3px;"></i>添加</el-button>
                <el-button type="text" size="small" @click="addStatutoryHolidays()">
                    <i class="iconfont icon-tianjia" style="margin-right: 3px;"></i>添加法定节假日</el-button>
                <el-button id="button-danger" type="text" icon="el-icon-delete" size="small"
                    :disabled="dataListSelections.length <= 0" @click="deleteHandle()">批量删除</el-button>
            </div>

            <div class="tableDisplayDiv">
                <div class="tableDiv">
                <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle"
                    style="width: 100%;" height="100%">
                    <el-table-column type="selection" header-align="center" align="center" width="50">
                    </el-table-column>
                    <el-table-column prop="name" header-align="center" align="center" label="节日名称">
                    </el-table-column>
                    <el-table-column prop="startDate" header-align="center" align="center" label="起始日期"
                        :formatter="formatDate">
                    </el-table-column>
                    <el-table-column prop="endDate" header-align="center" align="center" label="截止日期"
                        :formatter="formatDate">
                    </el-table-column>
                    <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
                    </el-table-column>
                    <el-table-column prop="updateTime" header-align="center" align="center" label="更新时间">
                    </el-table-column>
                    <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                            <el-button id="button-danger" type="text" size="small"
                                @click="deleteHandle(scope.row.id)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                </div>
                <pagination background @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
                    :current-page="pageIndex" :page-sizes="[10, 20, 30, 40]"
                    layout="total, sizes, prev, pager, next, jumper" :total="totalPage">
                </pagination>
            </div>
            
            
            <!-- 弹窗, 新增 / 修改 -->
            <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
        </div>
    </div>
</template>
  
<script>
import festivalApi from '@/api/enterprise/festival'
import AddOrUpdate from './festival-add-or-update'
export default {
    data() {
        return {
            dataForm: {
                key: ''
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            dataListLoading: false,
            dataListSelections: [],
            addOrUpdateVisible: false
        }
    },
    components: {
        AddOrUpdate
    },
    created() {
        this.getDataList()
    },
    methods: {
        // 获取数据列表
        getDataList() {
            this.dataListLoading = true;
            let params = {
                'page': this.pageIndex,
                'limit': this.pageSize,
                'key': this.dataForm.key
            };

            festivalApi.list(params)
                .then(res => {
                    // console.log(JSON.stringify(res))
                    if (res.code === this.ResultCode.success) {
                        this.dataList = res.page.list
                        this.totalPage = res.page.totalCount
                    } else {
                        this.dataList = []
                        this.totalPage = 0
                    }
                    this.dataListLoading = false
                })
        },
        // 每页数
        sizeChangeHandle(val) {
            this.pageSize = val
            this.pageIndex = 1
            this.getDataList()
        },
        // 当前页
        currentChangeHandle(val) {
            this.pageIndex = val
            this.getDataList()
        },
        // 多选
        selectionChangeHandle(val) {
            this.dataListSelections = val
        },
        // 新增 / 修改
        addOrUpdateHandle(id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
        // 删除
        deleteHandle(id) {
            var ids = id ? [id] : this.dataListSelections.map(item => {
                return item.id
            })
            this.$confirm(`确定进行 ${id ? '删除' : '批量删除'} 操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                festivalApi.deleteBatch(ids)
                    .then(res => {
                        if (res.code === this.ResultCode.success) {
                            this.getDataList()
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                            })
                        } else {
                            this.$message.error(data.msg)
                        }
                    })
            })
        },
        /**
         * 格式化日期
         */
        formatDate(row, column, cellValue, index) {
            let date = new Date(cellValue);
            return (date.getMonth() + 1) + "-" + date.getDate();
        },
        /**
         * 清除表单
         */
        resetQuery() {
            this.dataForm = [];
        },
        /**
         * 添加法定节假日
         */
        addStatutoryHolidays() {
            festivalApi.addStatutoryHolidays().then(
                res => {
                    this.$message.success("添加成功");
                    this.getDataList();
                }
            )
        }
    }
}
</script>
<style lang="scss" scoped>
.festivalContainer {
    padding: 20px 20px 0px 20px;
    // background: rgb(243, 5, 183);
    height: 100%;

    .contentDiv {
        background: #D3E6FB;
        border-radius: 5px;
        padding: 1px;
        height: 100%;

        .titleDiv {
            display: flex;
            align-items: center;
            padding: 5px 15px 5px 0px;
            height: 45px;
        }

        .toolDiv {
            background: #FFFFFF;
            padding: 1px;
            padding-left: 15px;
            height: 35px;
            display: flex;
            align-items: center;
        }

        .tableDisplayDiv {
            background: #FFFFFF;
            // height: 300px;
            height: calc(100% - 35px - 45px);

            .tableDiv {
                background: #FFFFFF;
                // height: 300px;
                height: calc(100% - 40px);
            }
        }

    }
}
</style>
  